﻿
<HTML xmlns="http://www.w3.org/1999/xhtml">
<HEAD>
<META content="IE=11.0000" http-equiv="X-UA-Compatible">
<TITLE>ZJCA客户端验证时间戳 DEMO </TITLE>     
<META http-equiv="Content-Type" content="text/html; charset=utf-8">
<LINK href="style.css" rel="stylesheet" type="text/css"> 

<script src="../../zjca_cmt/jquery.min.js" type="text/javascript"></script>
<script src="../../zjca_cmt/zjcacmt.js" type="text/javascript"></script>
<script src="../../zjca_cmt/zjcacmt_cert.js" type="text/javascript"></script>
<script src="../../zjca_cmt/zjcacmt_key.js" type="text/javascript"></script>
<script src="../../zjca_cmt/zjcacmt_com.js" type="text/javascript"></script>
<script src="../../zjca_cmt/zjcacmt_npapi.js" type="text/javascript"></script>
<script src="../../zjca_cmt/zjcacmt_websocket.js" type="text/javascript"></script>
<script src="../../zjca_cmt/zjcacmt_def.js" type="text/javascript"></script>
<script src="../../zjca_cmt/zjcacmt_err.js" type="text/javascript"></script>
<SCRIPT type="text/javascript">
	/* ZJCA 客户端插件对象 */
	var g_ZjcaCMT = null;
		
	// 验证类型
	var g_tsVerifyType = 0;
		
	/*
	 *	页面初始化
	 */
	window.onload = function() {
		//创建ZJCA客户端控件对象
		g_ZjcaCMT = new zjca_CMT(null, null);
		
		//设置客户端交互协议，在初始之前设置生效
		g_ZjcaCMT.setProtocol(PROTOCOL_HTTPS);
		
		//初始化ZJCA控件对象
		var resp = g_ZjcaCMT.init(1, 1);
		if (resp.code != 0) {
			alert("初始化ZJCA客户端失败！\n错误信息：" + resp.msg);
		}else {
			//显示版本信息
			resp = g_ZjcaCMT.getVersion();
			if (resp.code == 0) {
				document.getElementById('txtInfo').innerHTML = "客户端版本：" + resp.res;
			}else {
				document.getElementById('txtInfo').innerHTML = "客户端版本：未知";
			}
		}
		
		OnSelVerifyChanged();
	}		
	/*
	 *	页面关闭
	 */
	window.onunload = function () {
		/* 释放接口 */
		if (g_ZjcaCMT) {
			g_ZjcaCMT.finaled();
		}
	} 
	/*
	 *	验证类型改变
	 */
	function OnSelVerifyChanged() {
		g_tsVerifyType = document.getElementById("selVerifyTypes").value;	
		g_tsVerifyType = Number(g_tsVerifyType);
		
		// 如果是只验证时间戳、或者验证杂凑，则不需要选择杂凑算法
		if (1 == g_tsVerifyType) {
			document.form1.selDigestAlgs.disabled = true;
			document.form1.txtInput.disabled = true;
		}else if (2 == g_tsVerifyType) {
			document.form1.selDigestAlgs.disabled = true;	
			document.form1.txtInput.disabled = false;		
		}else {
			document.form1.selDigestAlgs.disabled = false;	
			document.form1.txtInput.disabled = false;		
		}			
	} 
	/*
	 *	解析时间戳
	 */
	 function on_btnTSDecode() {
		var tsText = document.form1.txtTimestamp.value;
		if (tsText.length == 0) {
			alert("请输入Base64格式的时间戳！");
			return;
		}
		
		// 解析时间戳,获取属性
		var resp = g_ZjcaCMT.parseTimestamp(tsText, "cert|digest_alg|digest|sign|time");
		if (resp.code != 0) {
			ShowErrorMessage("时间戳解析失败！", resp.code);			
			return;
		}
		
		var txtString;
		
		//获取时间戳里的证书
		document.form1.txtTSCert.value = resp.res.cert;
		
		//获取时间戳里的杂凑算法
		switch(Number(resp.res.digest_alg)){
			case 0x200:
				txtString = "杂凑算法:MD5";
				break;
			case 0x400:
				txtString = "杂凑算法:SHA1";
				break;
			case 0x800:
				txtString = "杂凑算法:SHA256";
				break;
			case 0x1000:
				txtString = "杂凑算法:SHA384";
				break;
			case 0x2000:
				txtString = "杂凑算法:SHA512";
				break;
			case 0x4000:
				txtString = "杂凑算法:SM3";
				break;
			case 0x4000:
				txtString = "杂凑算法:未知";
				break;
		}
		txtString += "\r\n";
		
		//获取时间戳里的杂凑值
		txtString += "杂凑值:" + resp.res.digest;
		document.form1.txtDigest.value = txtString;
		
		//获取时间戳里的签名值				
		document.form1.txtTSSignature.value = resp.res.sign;
		
		//获取时间戳里的时间
		document.form1.txtTime.value = resp.res.time;
		
		alert("时间戳解析成功！");		 
	 }		 
	/*
	 *	验证时间戳
	 */
	 function on_btnTSVerify() {
		var tsText = document.form1.txtTimestamp.value;
		if (tsText.length == 0) {
			alert("请输入Base64格式的时间戳！");
			return;
		}
		var certText = document.form1.txtTSCert.value;
		if (certText.length == 0) {
			alert("请输入服务器证书！");
			return;
		}
		
		var digestAlg;
		var txtString;
		var resp;
			
		// 验证时间戳
		switch(g_tsVerifyType) {
		case 1:	// 只验证时间戳
			resp = g_ZjcaCMT.verifyTimestamp(tsText, certText);
			break;
		case 2:
			txtString = document.form1.txtInput.value;
			if (txtString.length == 0) {
				alert("请输入Base64格式的杂凑值！");
				return;
			}
			resp = g_ZjcaCMT.verifyTimestampWithDigest(tsText, txtString, 0, certText);
			break;
		case 3:
			txtString = document.form1.txtInput.value;
			if (txtString.length == 0) {
				alert("请输入原文！");
				return;
			}
			digestAlg = document.getElementById("selDigestAlgs").value;	
			digestAlg = Number(digestAlg);
			resp = g_ZjcaCMT.verifyTimestampWithMessage(tsText, txtString, digestAlg, certText, "", "");
			break;
		default:
			alert("选择错误！");
			return;
		};
		if (resp.code == 0) {
			alert("时间戳有效！");
		}else {
			ShowErrorMessage("时间戳解析失败！", resp.code);			
		}
	 }
	
	//	显示错误信息函数
	function ShowErrorMessage(msg, code) {
		var errCode = zjca_GetErrHexCode(code);
		var errTxt = msg.length == 0 ? zjca_GetErrTxt(code) : msg;
	
		var errMsg = "错误代码：";
		errMsg += errCode;
		errMsg += "\n";
		errMsg += "错误信息：";
		errMsg += errTxt;
		alert(errMsg);
	}
</SCRIPT> 
<META name="GENERATOR" content="MSHTML 11.00.9600.17728">
</HEAD> 
<!---------------------------------------------------------------------------------------------------------------------------------> 
<!---------------------------------------------------------------------------------------------------------------------------------> 
<!---------------------------------------------------------------------------------------------------------------------------------> 
<!---------------------------------------------------------------------------------------------------------------------------------> 
<!---------------------------------------------------------------------------------------------------------------------------------> 
<BODY>
	<BR>
	<BR>
    <H2 align="center">ZJCA客户端验证时间戳 DEMO</H2>
    <FORM name="form1" action="">
	<fieldset>
		<legend>客户端信息</legend>
		<table>
			<colgroup>
				<col class="col1">
				<col class="col2">
				<col class="col3">
			</colgroup><tbody>
				<tr>
					<td></td>
					<td></td>
					<td></td>
				</tr>
				<tr>
					<td><label name="txtInfo" id="txtInfo" width="500" >未检测到ZJCA客户端程序！</label></td>
				</tr>
			</tbody>
		</table>
	</fieldset>
	<br>
    <FIELDSET>
        <LEGEND>解析时间戳</LEGEND>
        <TABLE>
            <TBODY>
            <TR>
				<TR>         
					<TD>
					时间戳:<TEXTAREA name="txtTimestamp" rows="5" cols="60"></TEXTAREA>
					</TD>
				</TR>
				<TR>
					<TD><INPUT name="btnTSDecode" class="decodeBtn" onclick="on_btnTSDecode();" type="button" value="解析时间戳"></TD> 
				</TR>
				<TR>					 
					<TD>
					证书:<TEXTAREA name="txtTSCert" rows="5" cols="60"></TEXTAREA>
					</TD>
				</TR>
				<TR>					 
					<TD>
					杂凑:<TEXTAREA name="txtDigest" rows="5" cols="60"></TEXTAREA>
					</TD>
				</TR>
				<TR>
					<TD>
					签名:<TEXTAREA name="txtTSSignature" rows="5" cols="60"></TEXTAREA>
					</TD>
				</TR>
				<TR>
					<TD>
					时间:<INPUT name="txtTime" type="text" readonly="readonly">
					</TD>
				</TR>
            </TR>
            </TBODY>
        </TABLE>
    </FIELDSET>
	<BR>	
    <FIELDSET>
        <LEGEND>验证时间戳</LEGEND>
        <TABLE>
            <TBODY>
            <TR>
				<TR>
				<TD style="vertical-align:top;">
					校验类型：
					<SELECT name="selVerifyTypes" id="selVerifyTypes" onchange="OnSelVerifyChanged()" readonly="true" style="width:150px;margin-left:5px;">
						<option value = 1>只校验时间戳</Option>
						<option value = 2>校验时间戳和杂凑</Option>
						<option value = 3>校验时间戳和原文</Option>
					</SELECT>
					杂凑算法：
					<SELECT name="selDigestAlgs" id="selDigestAlgs" readonly="true" style="width:100px;margin-left:5px;">
						<option value = 0x200>MD5</Option>
						<option value = 0x400>SHA1</Option>
						<option value = 0x800>SHA256</Option>
						<option value = 0x4000>SM3</Option>
					</SELECT>
				</TD>
				</TR>
				<TR>         
					<TD>
					原文或杂凑:<TEXTAREA name="txtInput" rows="5" cols="60"></TEXTAREA>
					</TD>
				</TR>
				<TR>
					<TD><INPUT name="btnTSVerify" class="verifyBtn" onclick="on_btnTSVerify();" type="button" value="验证时间戳"></TD> 
				</TR>
            </TR>
            </TBODY>
        </TABLE>
    </FIELDSET>
	<BR>
    </FORM>
</BODY>
</HTML>
